


import 'dart:convert';

import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/widget/notifier_widget.dart';
import 'package:bike_ride/model/freight/view/freight_v.dart';
import 'package:bike_ride/model/main/model/area_info_m.dart';
import 'package:bike_ride/model/unlock/model/bike_info_m.dart';
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';


class FreightViewModel extends PageViewModel<FreightViewState> {
  CancelToken? cancelToken;
  AreaInfoM? serverInfo;
  FeeInfo? bikeFeeInfo;
  Map<String, dynamic> startInfo = {};
  Map<String, dynamic> timeInfo = {};
  Map<String, dynamic> openList = {};
  /// dynamic:Map<String, dynamic>
  List<dynamic> billingPriceInfo = [];
  List<dynamic> weekendPriceInfo = [];
  bool locationAuthVisible = false;

  @override
  onCreate() {
    cancelToken = CancelToken();
    pageDataModel = PageDataModel();
    if(state.widget.arguments?['vid'] != null){
      getBikeInfo(state.widget.arguments!['vid']!);
    }else{
      getBrandAreaInfo();
    }
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }

  Future getBikeInfo(String vid) async {
    EasyLoading.show();
    final result = await CommonRepository.getBikeInfoIdl(cancelToken: cancelToken,vid: vid);
    EasyLoading.dismiss();
    if (result != null) {
      if(result.bikeInfo?.areaId != null && result.bikeInfo!.areaId!.isNotEmpty){
        AccountManager.handleAreaCode(tBikeAreaCode: result.bikeInfo?.areaId,refreshBrandInfoBol:false);
        getBrandAreaInfo();
      }
      bikeFeeInfo = result.feeInfo;
    }
    state.notifyChange();
  }


  Future getBrandAreaInfo() async {
    final result = await CommonRepository.getBrandAreaInfoIdl(cancelToken: cancelToken);
    if (result != null) {
      AccountManager.areaInfoM = result;
      serverInfo = result;

      ///分时段计费
      List<dynamic> priceInfo = [];
      Map<String, dynamic> mWeekendPriceInfo = {'display': []};
      try {
        priceInfo = jsonDecode((serverInfo!.priceTime == null || serverInfo!.priceTime!.isEmpty)? '[]' : serverInfo!.priceTime!);
        mWeekendPriceInfo = jsonDecode((serverInfo!.weekPriceTime == null || serverInfo!.weekPriceTime!.isEmpty)?'{"display": []}':serverInfo!.weekPriceTime!);
      } catch (error) {
        priceInfo = [];
        mWeekendPriceInfo = {'display': []};
      }
      if (serverInfo!.isBillingDuration == 1 && priceInfo.isNotEmpty) {
        billingPriceInfo = priceInfo;
      }
      if (serverInfo!.isBillingDuration == 3 && mWeekendPriceInfo['display']!.isNotEmpty) {
        weekendPriceInfo = mWeekendPriceInfo['display'];
      }

      startInfo = {
        'price': serverInfo!.startPrice,
        'time': serverInfo!.startMin,
      };
      timeInfo = {
        'price': serverInfo!.everyPrice,
        'time': serverInfo!.everyMin,
      };
    }
    state.notifyChange();
  }


  /// 请求数据
  @override
  Future<PageViewModel?> requestData({Map<String, dynamic>? params}) async {
    return null;

  }
}